<?php
    echo require_js(array( $CFG->wwwroot.'/blocks/widget_store/lib/jquery-1.7.2.min.js'));
    
    //$gallery = new widget_gallery();
    //print($gallery->showGallery());
?>
<div class="widget_store_block" align="left">
<div class="toolbar">
<div class="toolbar_placard">Widget Discovery Tools</div>
<div class="searchtag" onclick="searchWidgets('*:*')">All Widgets</div>
<div class="searchtag" onclick="getLatestWidgets()">Latest Widgets</div>
<div class="searchtag" onclick="getFeaturedWidgets()">Featured</div><div style="float:left">&nbsp;</div>
<input class="searchbox" type="text" name="q" placeholder="Search term here" id="searchText"/>
<div class="searchtag" onclick="searchStore();">Search</div>
<div style="clear:both"></div>
</div>
<div style="clear:both">&nbsp;</div>
<div id="gallery" class="widget_list"></div>
</div>
<script type="text/javascript">

var rest_url = "<?php echo $CFG->block_widget_store_rest_url;?>";
var moodleroot = "<?php echo $CFG->wwwroot;?>";
/*jQuery(document).ready(function() {

   $('.wookie-button').mouseover(function(){
      $(this).css("backgroundColor","#2888D8");
   });

   $('.wookie-button').mouseout(function(){
      $(this).css("backgroundColor","#2878C0");
   });
   
    jQuery('#widget_gallery').click( function(e){
        var $clicked_element = $(e.target)
        if ( $clicked_element.is('input') ){
            e.preventDefault();
            var widget_id = $clicked_element.attr('id');
            var widget_type = $clicked_element.attr('name')
            $("#widget_id").val(widget_id);  
            $("form[name='block-config']").submit();       
        }
    });

});*/

$(document).ready(function() {
	jQuery.support.cors = true;
	searchWidgets("*:*");
	$("#searchText").on('keydown', function(e) {
		if ( e.which == 13 ) {
			e.preventDefault();
			searchStore();
		}
	});
});


function searchStore( ) {
	var searchString = $("#searchText").val();
	searchWidgets(searchString);
}


function searchWidgets(searchString ) {
	$.ajax({
		type:"GET",
		url:rest_url+"/widgets/search/"+searchString+"/0/500",
		dataType:"text",
		success:parseResultAndBuildGallery,
		error:searchError
	});
}

function getLatestWidgets() {
	$.ajax({
		type:"GET",
		url:rest_url+"/widgets/search/*:*/0/50/date",
		dataType:"text",
		success:parseResultAndBuildGallery
	});
}

function getFeaturedWidgets() {
	$.ajax({
		type:"GET",
		url:rest_url+"/widgets/featured",
		dataType: "text",
		success: parseResultAndBuildGallery
	});
}


function searchError(jqXHR, textStatus, errorThrown) {
	alert ( "Error searching for widgets, status: "+jqXHR.status);
}


function parseResultAndBuildGallery(jsonString) {
	searchedWidgets = [];
	parseAndCopyWidgetList(jsonString, searchedWidgets);
	renderGallery(searchedWidgets);
}


function renderGallery ( widgetlist ) {
	var html = '';
	for ( var i = 0; i < widgetlist.length; i++ ) {
		var widget = widgetlist[i];
		html += '<div class="widget_link" onclick="selectWidget(\''+widget.uri+'\')">';
		html += '<img src="'+widget.icon+'" width="16" height="16"/>';
		html += '&nbsp;<span class="widgetTitle">'+widget.name+'</span>';
		html += '</div>';
	}
	html += '<div class="clear">&nbsp;</div>'
	$("#gallery").html(html);
}

function parseAndCopyWidgetList ( inJSON, outWidgetList ) {
	var obj = $.parseJSON(inJSON);
	var widgets = obj.widgets;
	if ( widgets == null ) {
		// try SearchResults returned by later versions of edukapp
		widgets = obj.SearchResults;
		if ( widgets == null ) {
			// try just getting top level, might just be an array
			widgets = obj;
		}
	}
	if ( $.isArray(widgets)) {
		for ( var i = 0; i < widgets.length; i++ ) {
			var widget = widgets[i];
			if ( widget.icon == null ) {
				widget.icon = moodleroot+"/blocks/widget_store/img/default_widget_icon.png";
			}
			outWidgetList.push ( widget );
		}
	}
}

function selectWidget ( widgetId ) {
	$("#widget_id").val(widgetId);
	$("form[name='block-config']").submit();
}
</script>

<input type="hidden" name="widget_id" id="widget_id"/>
